package com.is.control;

import java.sql.ResultSet;

import com.is.dbconn.DBConnect;
import com.is.model.ParamReasonTypeGS;

public class ParamReasonTypeGSControl {
	
	
	public static boolean insertGS(ParamReasonTypeGS gs) {
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into param_reasontype_gs(reasonTypeId,num,stuyear) values(?,?,?,?)";
		boolean flag = false;
		dbConnect.prepareStatement(sql);
		try {
			dbConnect.setInt(1, gs.getReasonTypeId());
			dbConnect.setInt(2, gs.getNum());
			dbConnect.setInt(3, gs.getStuyear());
			flag = dbConnect.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("insertGS　ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	

	public static boolean updateGS(ParamReasonTypeGS gs,int id) {
		DBConnect dbConnect = new DBConnect();
		String sql = "update param_reasontype_gs set reasonTypeId=?,num=?,stuyear=? where id=?";
		boolean flag = false;
		dbConnect.prepareStatement(sql);
		try {
			dbConnect.setInt(1, gs.getReasonTypeId());
			dbConnect.setInt(2, gs.getNum());
			dbConnect.setInt(3, gs.getStuyear());
			dbConnect.setInt(4,id);
			flag = dbConnect.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("updateGS　ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	
	public  static int getNumByTypeAndStuyear(int type,int stuyear) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select num from param_reasontype_gs where stuyear='"+stuyear+"' and reasonTypeId = '"+type+"'";
		int num = 0;
		ResultSet rs = null;
		dbConnect.prepareStatement(sql);
		try {
			rs =dbConnect.executeQuery();
			if(rs.next()){
				num = rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("getNumByTypeAndStuyear ERROR");
		}finally{
			dbConnect.close();
		}
		return num;
	}
	
	public static boolean isInserted(int stuyear) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select id from param_reasontype_gs where stuyear='"+stuyear+"' ";
		dbConnect.prepareStatement(sql);
		ResultSet rs = null;
		boolean flag = false;
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("isInserted ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}

}
